#include "osObjects.h"  
#include "systimer.h"
#include "rtctime.h"
#include "pincfg.h"

uint8_t KeyLast[2];
uint8_t KeyVal,KeyValLast;

#define KEY0_MSK 0x01
#define KEY1_MSK 0x02
#define KEY2_MSK 0x03

uint32_t IdleTaskCnt=0;

uint32_t KeyCnt=0;

/*----------------------------------------------------------------------------
 *      Global Functions
 *---------------------------------------------------------------------------*/

/*--------------------------- os_idle_demon ---------------------------------*/
   
/*
const uint8_t SysData[16][128] __attribute__ ((at(0x08008000))) = 
{
    "What a lovely day !",
	"Hellow, where are you ?",
};
*/

/// \brief The idle demon is running when no other thread is ready to run
void os_idle_demon (void)
{    
    uint8_t KeyRead;
    
	{
		SysPulseClear();
		SysPulseCreat();
        if(bPulse_1s){
            TimeRun();
        }
        IdleTaskCnt++;
        
        if(bPulse_10ms){
            KeyRead = 0;
            if(Key0St()) KeyRead |= KEY0_MSK;
            if(Key1St()) KeyRead |= KEY1_MSK;
            if(Key2St()) KeyRead |= KEY2_MSK;
            
            if((KeyLast[0]==KeyRead) && (KeyLast[1]==KeyRead))
                KeyVal = KeyRead;
            
            KeyLast[1] = KeyLast[0];
            KeyLast[0] = KeyRead;
        }
        
        switch(KeyVal){
        case KEY0_MSK:
            KeyCnt+=1;
            break;
        case KEY1_MSK:
            KeyCnt+=2;
            break;
        case KEY2_MSK:
            KeyCnt+=3;
            break;
        default :break;
        }
        
        if(KeyVal != KeyValLast){
            if(KeyVal){
                printf(" Key pressed !\n");
            } else {
                printf(" Key released !\n");
            }
        }
        
        KeyValLast = KeyVal;
    }
}

